package com.skywolf.chem.tencent.utils;


import java.io.InputStream;

import com.qcloud.cos.model.*;
import com.skywolf.chem.utils.SysConfig;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONObject;
import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.region.Region;


/**
 * Created by zh on 2018/7/14.
 */
public class UploadFileUtils {
    private static Logger logger = LoggerFactory.getLogger(UploadFileUtils.class);

    public static String uploadPicNew(String uid, InputStream inputStream, String fileName, String imgType, String path) {
        String bucket = SysConfig._this.getProperty("tencent_cdn.bucket");
        String app_id = SysConfig._this.getProperty("tencent_cdn.app_id");
        String secret_id = SysConfig._this.getProperty("tencent_cdn.secret_id");
        String secret_key = SysConfig._this.getProperty("tencent_cdn.secret_key");

        String etag = "";
        // 1 初始化用户身份信息(secretId, secretKey)
        COSCredentials cred = new BasicCOSCredentials(secret_id, secret_key);
        // 2 设置bucket的区域, COS地域的简称请参照 https://www.qcloud.com/document/product/436/6224
        ClientConfig clientConfig = new ClientConfig(new Region("ap-chengdu"));
        // 3 生成cos客户端
        COSClient cosclient = new COSClient(cred, clientConfig);
        // bucket名需包含appid
        String bucketName = bucket + "-" + app_id;
        try {
            // 创建上传Object的Metadata
            ObjectMetadata objectMetadata = new ObjectMetadata();
            objectMetadata.setContentLength(inputStream.available());
            objectMetadata.setCacheControl("max-age=25920000");
           // objectMetadata.setCacheControl("no-cache");
          //  objectMetadata.setHeader("Pragma", "no-cache");
            objectMetadata.setContentType(imgType);
            objectMetadata.setContentDisposition("inline;filename=" + fileName);
            if (StringUtils.isNotBlank(path)) {
                fileName = path + "/" + fileName;
            }
            // 上传文件
            PutObjectResult putResult = cosclient.putObject(bucketName, fileName, inputStream, objectMetadata);
            etag = fileName;
        } catch (Exception e) {
            logger.error("upload pic new fail,uid:{},fileName:{},imgType:{},path:{},error message:{}", uid, fileName, imgType, path, e.getMessage(), e);
        }
        // 关闭客户端
        cosclient.shutdown();
        return etag;
    }

    public static String uploadPicNew(InputStream inputStream, String fileName, String imgType, String path) {
        return uploadPicNew(null, inputStream, fileName, imgType, path);
    }

    public static void main(String[] args) {
        //批量更新no-cache
        String bucket = "wukong";
        String app_id = "1259136015";
        String secret_id = "AKIDBWczW1v3TlsnTyBdCdsxHRm01pJXa3Zs";
        String secret_key = "9sQtIcr3ZU2AOeJoHdTJqIQNXXJGF3c0";

        String etag = "";
        // 1 初始化用户身份信息(secretId, secretKey)
        COSCredentials cred = new BasicCOSCredentials(secret_id, secret_key);
        // 2 设置bucket的区域, COS地域的简称请参照 https://www.qcloud.com/document/product/436/6224
        ClientConfig clientConfig = new ClientConfig(new Region("ap-chengdu"));
        // 3 生成cos客户端
        COSClient cosclient = new COSClient(cred, clientConfig);
        // bucket名需包含appid
        String bucketName = bucket + "-" + app_id;
        try {
            ObjectListing list= cosclient.listObjects(bucketName);
            for (COSObjectSummary s : list.getObjectSummaries()) {
                if(s.getKey().startsWith("wukong_head")){
                    COSObject obj= cosclient.getObject(bucketName,s.getKey());
                    obj.getObjectMetadata().setContentType("image/jpeg");
                    obj.getObjectMetadata().setCacheControl("max-age=8640000");
                    cosclient.updateObjectMetaData(bucketName,s.getKey(),obj.getObjectMetadata());
                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 关闭客户端
        cosclient.shutdown();
    }


}
